﻿using System;
using System.Data;
using System.Data.SqlClient ;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;


namespace AnyOffice.general.hrms.query
{
    public partial class query : ModulePage
    {


        private string strTemp = "";  
       
        private void LoadDeptTree(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.LoadDeptTree(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ListItem item = new ListItem("全部", "");
                this.ddlDeptId.Items.Add(item);
                DataTable table = new DepartmentDao().SelectParentAll(base.Conn);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                        this.strTemp = "";
                        this.LoadDeptTree(ref this.ddlDeptId, int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp);
                    }
                }
                this.ddlDeptId.Items.Add(new ListItem("外部/离校人员", "-1"));
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                this.litNavTitle.Text = "学生档案查询结果";
                ArrayList list = new ArrayList();
                StringBuilder builder = new StringBuilder();
                builder.Append("SELECT [HrmsId],[UserId],[UserName],[DeptId],[Sex],[Birthday],[Nation],[Marry],[NativePlace],[Politics],[CardNo] ");
                builder.Append(" FROM [dbo].[Hrms] WHERE (1=1) ");
                if (this.ddlDeptId.SelectedValue != string.Empty)
                {
                    builder.Append(" AND (DeptId = @DeptId) ");
                    list.Add(new object[] { "@DeptId", Convert.ToInt32(this.ddlDeptId.SelectedValue) });
                }
                if (this.txtNumber.Value != string.Empty)
                {
                    builder.Append(" AND (Number = @Number) ");
                    list.Add(new object[] { "@Number", this.txtNumber.Value });
                }
                if (this.selectSex.Value != string.Empty)
                {
                    builder.Append(" AND (Sex = @Sex) ");
                    list.Add(new object[] { "@Sex", Convert.ToBoolean(this.selectSex.Value) });
                }
                if (this.txtMinAge.Value != string.Empty)
                {
                    builder.Append(" AND (DATEDIFF(year,Birthday,getdate()) >= @MinAge) ");
                    list.Add(new object[] { "@MinAge", Convert.ToInt32(this.txtMinAge.Value) });
                }
                if (this.txtMaxAge.Value != string.Empty)
                {
                    builder.Append(" AND (DATEDIFF(year,Birthday,getdate()) <= @MaxAge) ");
                    list.Add(new object[] { "@MaxAge", Convert.ToInt32(this.txtMaxAge.Value) });
                }
                if (this.txtNation.Value != string.Empty)
                {
                    builder.Append(" AND (Nation = @Nation) ");
                    list.Add(new object[] { "@Nation", this.txtNation.Value });
                }
                if (this.txtCardNo.Value != string.Empty)
                {
                    builder.Append(" AND (CardNo = @CardNo) ");
                    list.Add(new object[] { "@CardNo", this.txtCardNo.Value });
                }
                if (this.selectMarry.Value != string.Empty)
                {
                    builder.Append(" AND (Marry = @Marry) ");
                    list.Add(new object[] { "@Marry", this.selectMarry.Value });
                }
                if (this.txtNativePlace.Value != string.Empty)
                {
                    builder.Append(" AND NativePlace LIKE '%' + @NativePlace + '%' ");
                    list.Add(new object[] { "@NativePlace", this.txtNativePlace.Value });
                }
                if (this.txtHukouPlace.Value != string.Empty)
                {
                    builder.Append(" AND HukouPlace LIKE '%' + @HukouPlace + '%' ");
                    list.Add(new object[] { "@HukouPlace", this.txtHukouPlace.Value });
                }
                if (this.selectEducation.Value != string.Empty)
                {
                    builder.Append(" AND (Education = @Education) ");
                    list.Add(new object[] { "@Education", this.selectEducation.Value });
                }
                if (this.txtSpeciallity.Value != string.Empty)
                {
                    builder.Append(" AND (Speciallity LIKE '%' + @Speciallity + '%') ");
                    list.Add(new object[] { "@Speciallity", this.txtSpeciallity.Value });
                }
                if (this.txtSchool.Value != string.Empty)
                {
                    builder.Append(" AND (School LIKE '%' + @School + '%') ");
                    list.Add(new object[] { "@School", this.txtSchool.Value });
                }
                if (this.txtWorkDateBegin.Value != string.Empty)
                {
                    builder.Append(" AND (WorkDate >= @WorkDateBegin) ");
                    list.Add(new object[] { "@WorkDateBegin", Convert.ToDateTime(this.txtWorkDateBegin.Value + " 00:00:00") });
                }
                if (this.txtWorkDateEnd.Value != string.Empty)
                {
                    builder.Append(" AND (WorkDate <= @WorkDateEnd) ");
                    list.Add(new object[] { "@WorkDateEnd", Convert.ToDateTime(this.txtWorkDateEnd.Value + " 23:59:59") });
                }
                if (this.txtJoinDateBegin.Value != string.Empty)
                {
                    builder.Append(" AND (JoinDate >= @JoinDateBegin) ");
                    list.Add(new object[] { "@JoinDateBegin", Convert.ToDateTime(this.txtJoinDateBegin.Value + " 00:00:00") });
                }
                if (this.txtJoinDateEnd.Value != string.Empty)
                {
                    builder.Append(" AND (JoinDate <= @JoinDateEnd) ");
                    list.Add(new object[] { "@JoinDateEnd", Convert.ToDateTime(this.txtJoinDateEnd.Value + " 23:59:59") });
                }
                if (this.txtPost.Value != string.Empty)
                {
                    builder.Append(" AND (Post LIKE '%' + @Post + '%') ");
                    list.Add(new object[] { "@Post", this.txtPost.Value });
                }
                if (this.txtTechPost.Value != string.Empty)
                {
                    builder.Append(" AND (TechPost LIKE '%' + @TechPost + '%') ");
                    list.Add(new object[] { "@TechPost", this.txtTechPost.Value });
                }
                if (this.selectPolitics.Value != string.Empty)
                {
                    builder.Append(" AND (Politics = @Politics) ");
                    list.Add(new object[] { "@Politics", this.selectPolitics.Value });
                }
                if (this.txtMemo.Value != string.Empty)
                {
                    builder.Append(" AND (Memo LIKE '%' + @Memo + '%') ");
                    list.Add(new object[] { "@Memo", this.txtMemo.Value });
                }
                builder.Append(" ORDER BY [HrmsId] ASC");
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int j = 0; j < list.Count; j++)
                {
                    object[] objArray22 = (object[])list[j];
                    commandParameters[j] = new SqlParameter(objArray22[0].ToString(), objArray22[1]);
                }
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn,CommandType.Text , builder.ToString(), commandParameters).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.pnlResult.Visible = true;
                    this.rptHrms.DataSource = table2;
                    this.rptHrms.DataBind();
                }
                else
                {
                    this.litMessage.Text = base.ShowPrompt("无符合条件的学生档案信息", "query.aspx");
                }
            }
        }
 

    }
}
